﻿using System.Collections.Generic;
using Microsoft.ApplicationServer.Caching;

namespace HoangPhap.Utilities
{
    public class CacheUtil
    {
        private static DataCacheFactory _factory;
        private static DataCache _cache;

        public static DataCache GetCache()
        {
            if (_cache != null)
                return _cache;

            //Define Array for 1 Cache Host
            var servers = new List<DataCacheServerEndpoint>(1) {new DataCacheServerEndpoint(Configs.WebCacheServer, Configs.WebCacheServerPort)};

            //Specify Cache Host Details 
            //  Parameter 1 = host name
            //  Parameter 2 = cache port number

            //Create cache configuration
            var configuration = new DataCacheFactoryConfiguration
                {
                    Servers = servers,
                    LocalCacheProperties = new DataCacheLocalCacheProperties()
                };

            //Set the cache host(s)

            //Set default properties for local cache (local cache disabled)

            //Disable tracing to avoid informational/verbose messages on the web page
            DataCacheClientLogManager.ChangeLogLevel(System.Diagnostics.TraceLevel.Off);

            //Pass configuration settings to cacheFactory constructor
            _factory = new DataCacheFactory(configuration);

            //Get reference to named cache called "default"
            _cache = _factory.GetCache("default");

            return _cache;
        }
    }
}
